package mini.ideashare.cms.manager;

import mini.ideashare.cms.dao.CategoryDAO;
import mini.ideashare.cms.dao.TB_ItemDAO;
import mini.ideashare.cms.model.Category;
import mini.ideashare.cms.model.TB_Item;
import mini.ideashare.cms.model.qc.CategoryQC;
import mini.ideashare.cms.model.qc.Page;
import mini.ideashare.cms.model.qc.TB_ItemQC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class CategoryManager {

    @Autowired
    private CategoryDAO categoryDAO;

    public List<Category> queryCategory(Category category, Integer pageIndex , Integer pageSize){
        Page page = new Page(pageIndex,pageSize);
        CategoryQC qc  = new CategoryQC();
        qc.setPage(page);
        qc.setId(category.getId()).setName(category.getName());
        List<Category> categories = categoryDAO.listCategory(qc);
        return categories;
    }

    public Long countCategory(Category category){
        CategoryQC qc  = new CategoryQC();
        qc.setId(category.getId()).setName(category.getName());
        Long count = categoryDAO.countCategory(qc);
        return count;
    }

    public int saveCategory(Category category){

        if(category.getId()!=null){
            return categoryDAO.updateCategory(category);
        }else {
            category.setCreateTime(new Date());
            return categoryDAO.insertCategory(category);
        }

    }
    public int deleteCategory(Long id){

        return categoryDAO.deleteCategoryById(id);

    }



}
